﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using Sylvain.Model;

public partial class Admin_EditEmployees : System.Web.UI.Page
{
    //Instance of the SylvainManager Class
    ISylvainManager objManger = new SylvainManager();
    /// <summary>
    /// Bind Dropdownlist with Enums
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            BindEmployeesGrid();
        }
    }
    /// <summary>
    /// Bind NewsSection Details with Gridview
    /// </summary>
    protected void BindEmployeesGrid()
    {

        grvEmployees.DataSource = objManger.GetAllEmployees();
        grvEmployees.DataBind();
    }
    /// <summary>
    /// Create/Add New Record in NewsSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Employee objEmployee = new Employee();
            objEmployee.FirstName = this.txtFirstName.Text.Trim();
            objEmployee.LastName = this.txtLastName.Text.Trim();
            objEmployee.UserName = this.txtUserName.Text.Trim();
            objEmployee.Password = this.txtPassword.Text.Trim();
            objEmployee.Email = this.txtEmail.Text.Trim();
            objEmployee.Approved = this.chkApproved.Checked;
            objEmployee.CreatedDate = System.DateTime.Now;
            objEmployee.UpdatedDate = System.DateTime.Now;
            objManger.AddEmployee(objEmployee, System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Host"].ToString());


            BindEmployeesGrid();
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtEmail.Text = "";
            if (chkApproved.Checked)
            {
                lblError.Text = "Submitted Successfully. Email has been sent to the User.";
            }
            else
            {
                lblError.Text = "Submitted Successfully";
            }
            chkApproved.Checked = false;            
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("already exists"))
            {
                lblError.Text = ex.Message;
            }
            else
            {
                lblError.Text = "There is some error. please try again.";
            }
        }

    }
    /// <summary>
    /// Update Record in NewsSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            Employee objEmployee = new Employee();
            objEmployee.Id = int.Parse(hidId.Value);
            objEmployee.FirstName = this.txtFirstName.Text.Trim();
            objEmployee.LastName = this.txtLastName.Text.Trim();
            objEmployee.UserName = this.txtUserName.Text.Trim();
            objEmployee.Password = this.txtPassword.Text.Trim();
            objEmployee.Email = this.txtEmail.Text.Trim();
            objEmployee.Approved = this.chkApproved.Checked;
            objEmployee.CreatedDate = System.DateTime.Now;
            objEmployee.UpdatedDate = System.DateTime.Now;
            objManger.EditEmployee(objEmployee, System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Host"].ToString());


            BindEmployeesGrid();
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtEmail.Text = "";
            if (chkApproved.Checked)
            {
                lblError.Text = "Submitted Successfully. Email has been sent to the User.";
            }
            else
            {
                lblError.Text = "Submitted Successfully";
            }
            chkApproved.Checked = false;
            btnSubmit.Visible = true;
            btnUpdate.Visible = false;
        }
        catch(Exception ex)
        {
            if (ex.Message.Contains("already exists"))
            {
                lblError.Text = ex.Message;
            }
            else
            {
                lblError.Text = "There is some error. please try again.";
            }
        }
    }
    /// <summary>
    /// Delete/Update Record in Gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployees_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);

                //Delete News
                objManger.DeleteEmployee(ID);
                BindEmployeesGrid();
                txtFirstName.Text = "";
                txtLastName.Text = "";
                txtUserName.Text = "";
                txtPassword.Text = "";
                txtEmail.Text = "";
                lblError.Text = "";
                chkApproved.Checked = false;
                btnSubmit.Visible = true;
                btnUpdate.Visible = false;
            }
            if (e.CommandName == "Update")
            {
                hidId.Value = e.CommandArgument.ToString();

                btnSubmit.Visible = false;
                btnUpdate.Visible = true;

                lblError.Text = "";
            }
        }
        catch
        {
            lblError.Text = "Operation could not be performed.";
        }
    }
    /// <summary>
    /// Updating a Record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)grvEmployees.Rows[e.RowIndex];
            txtFirstName.Text = ((Label)row.FindControl("lblFirstName")).Text;
            txtLastName.Text = ((Label)row.FindControl("lblLastName")).Text;
            txtUserName.Text = ((Label)row.FindControl("lblUserName")).Text;
            txtPassword.Text = ((Label)row.FindControl("lblPassword")).Text;
            txtEmail.Text = ((Label)row.FindControl("lblEmail")).Text;
            chkApproved.Checked = Convert.ToBoolean(((Label)row.FindControl("lblApproved")).Text);
            btnSubmit.Visible = false;
            btnUpdate.Visible = true;
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Display Page Index
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvEmployees.PageIndex = e.NewPageIndex;
        BindEmployeesGrid();
    }
    /// <summary>
    /// Deleting a Rocord
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployees_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

}